<?php
namespace app\common\service\UserService;

use app\model\BbcModel\MemberBank;
use app\common\service\SmsService\SmsCodeService;
use app\model\BbcModel\Bank;
use app\model\BbcModel\ChannelBusiness;
use app\Normal;

class MemberBankService
{
    public function validate($param){
        if(empty($param['card_type'])){
            return Normal::returnCode(Normal::FAIL,"请选择银行卡类型");
        }
        if(empty($param['mid'])){
            return Normal::returnCode(Normal::FAIL,"mid必传");
        }
        if(empty($param['bankcard'])){
            return Normal::returnCode(Normal::FAIL,"请填入银行卡号");
        }
        if(empty($param['bank_id'])){
            return Normal::returnCode(Normal::FAIL,"请填入银行卡号");
        }
         if (empty($param['code'])) {
             return Normal::returnCode(Normal::FAIL,"验证码不能为空");
        }
         if(empty($param['mobile'])) {
            return Normal::returnCode(Normal::FAIL,"手机号码不能为空");
         }
        if($param['card_type']==1){
            if (empty($param['idcard'])) {
                return Normal::returnCode(Normal::FAIL,"身份证号码不能为空");
            }
            if (empty($param['true_name'])) {
            return Normal::returnCode(Normal::FAIL,"姓名不能为空");
        }
        }else{
            if (empty($param['org_name'])) {
                return Normal::returnCode(Normal::FAIL,"企业名称不能为空");
            }
            if (empty($param['org_code'])) {
                return Normal::returnCode(Normal::FAIL,"企业代码不能为空");
            }
        }
        return Normal::returnCode(Normal::SUC,"验证成功");
    }
    public function addCard($param)
    {
        if(!empty($param['cb_id'])){
            $cb= ChannelBusiness::instance()->info(['id'=>$param['cb_id']]);
            if(!$cb){
                return Normal::returnCode(Normal::FAIL,"渠道id错误");
            }
            $args = [
                'mobile'   => $cb['mobile'],
            ];
            $checkMember = callTcpService('bbcUserAccountInfo',$args);
            if($checkMember['code']==200){
                $mid=$checkMember['data']['mid'];
                $param['mid']=$mid;
            }else{
                return Normal::returnCode(Normal::FAIL,"SSO服务错误:".$checkMember['msg']);
            }
        }
        $check=$this->validate($param);
        if ($check['code'] != Normal::SUC) {
            return $check;
        }
        if(!empty($param['bankcard'])){
            $param['bankcard'] = str_replace(array("\r\n", "\r", "\n"), "", $param['bankcard']);
        }
        $rep= MemberBank::instance()->info(['mid'=>$param['mid'],'bankcard'=>$param['bankcard']]);
        if($rep){
            return Normal::returnCode(Normal::FAIL,"银行卡重复");
        }
        $sms_res = callApi(SmsCodeService::class, 'validate',$param);
        if ($sms_res['code'] != Normal::SUC) {
            return Normal::returnCode(Normal::FAIL,"验证码错误");
        }
        unset($param['code']);
        
        $param['bank_name']=Bank::instance()->where(['id'=>$param['bank_id']])->value('name');
        unset($param['cb_id']);
        unset($param['cid']);
        $res= MemberBank::instance()->add($param);
        if($res){
            return Normal::returnCode(Normal::SUC,"新增成功");
        }else{
            return Normal::returnCode(Normal::FAIL,"新增失败");
        }
    }

    /**
     * 获取银行卡列表
     */
    public function getList(array $param=[])
    {
        if(!empty($param['cb_id'])){
            $cb= ChannelBusiness::instance()->info(['id'=>$param['cb_id']]);
            if(!$cb){
                return Normal::returnCode(Normal::FAIL,"渠道id错误");
            }
            $args = [
                'mobile'   => $cb['mobile'],
            ];
            $checkMember = callTcpService('bbcUserAccountInfo',$args);
            if($checkMember['code']==200){
                $mid=$checkMember['data']['mid'];
                $param['mid']=$mid;
            }else{
                return Normal::returnCode(Normal::FAIL,"SSO服务错误:".$checkMember['msg']);
            }
        }
        $list= MemberBank::instance()->lists(['mid'=>$param['mid'],'df'=>0,'_fields_'=>'id,bankcard,bank_name,card_type']);
        foreach ($list as $k=>$v){
            //$list[$k]['bankcard']="**** **** **** ".substr($v['bankcard'],-4);
            $list[$k]['bank_name']=$v['bank_name'].substr($v['bankcard'],-4);
            $list[$k]['type']=MemberBank::instance()->type[$v['card_type']];
        }
        $data['list']=$list;
        return Normal::returnCode(Normal::SUC,"请求成功",$data);
    }

    
}